/**
 * @param {string} s
 * @return {number}
 */
var lengthOfLongestSubstring = function(s) {
  if(s.length <= 1)
    return s.length;
  let temp = new Array(256).fill(0), maxLength = 0, resStr = "";
  for(let i = 0; i < s.length; i++) {
    if(!temp[s[i].charCodeAt()]) {
      temp[s[i].charCodeAt()] = 1;
      resStr += s[i];
    } else {
      while(temp[s[i].charCodeAt()]) {
        temp[resStr[0].charCodeAt()] = 0;
        if(resStr.length == 1)
          resStr = "";
        else
          resStr = resStr.slice(1, resStr.length);
      }
      resStr += s[i];
      temp[resStr[resStr.length - 1].charCodeAt()] = 1;
    }
    if(resStr.length > maxLength) {
      maxLength = resStr.length;
    }
  }
  return maxLength;
};
let s = "abcabcbb";
console.log(lengthOfLongestSubstring(s));
s = "bbbbb";
console.log(lengthOfLongestSubstring(s));
s = "pwwkew";
console.log(lengthOfLongestSubstring(s));
s = "aab";
console.log(lengthOfLongestSubstring(s));
